package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.GuideDetailEvent;
import com.jwdf.uda.ui.adapter.viewholder.GuideCommentMultiImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideCommentSingleImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideCommentTxtViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideDesignerShowViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideInteractionMultiImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideInteractionSingleImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideInteractionTitleViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideInteractionTxtViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideLikeViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideSectionLikeViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideThumbLikeViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideTitleTabViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideUserMultiImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideUserSingleImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.GuideUserTxtViewHolder;
import com.jwdf.uda.ui.adapter.item.GuideDetailItem;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 */
public class GuideDetailAdapter extends AbsAdapter<GuideDetailItem> {
    private GuideDetailItem.GuideTab mCurrentTab = GuideDetailItem.GuideTab.COMMENT;
    private List<GuideDetailItem> mItems = new ArrayList<>();
    private List<GuideDetailItem> mItemsTab0 = new ArrayList<>();
    private List<GuideDetailItem> mItemsTab1 = new ArrayList<>();

    public GuideDetailAdapter(Context context) {
        super(context);
    }

    public void setCurrentTab(GuideDetailItem.GuideTab currentTab) {
        mCurrentTab = currentTab;
        for (int i = 0; i < mItems.size(); i++) {
            GuideDetailItem item = mItems.get(i);
            if (item.itemType == GuideDetailItem.TYPE_TITLE_TAB) {
                item.currentTab = currentTab;
            }
        }
        notifyDataSetChanged();
    }

    public GuideDetailItem.GuideTab getCurrentTab() {
        return mCurrentTab;
    }

    public int getCommentOffset() {
        return mItemsTab0.size();
    }

    public void setDetailData(PbBean.PbResGuideDetail detail) {
        mItems.clear();
        mItemsTab0.clear();
        mItemsTab1.clear();
        addUserItem(detail);
        addUserShowItem(detail.getDesignerInfo(), detail.getDetail());

        List<PbBean.PbGuideInteraction> interactions = detail.getInteractionListList();
        if (interactions != null && interactions.size() > 0) {
            addInteractionTitleItem(detail);
            for (int i = 0; i < interactions.size(); i++) {
                PbBean.PbGuideInteraction interaction = interactions.get(i);
                addInteractionItem(interaction, detail.getDesignerInfo().getUserId().equals(interaction.getUserInfo().getUserId()), detail.getDetail().getStatus());
            }
        }

        //搭配结束
//        if (detail.getDetail().getStatus() == PbBean.PbSignGuideStatus.GUIDE_STATUS_FINISHED) {
        if (detail.getLikeListCount() > 0) {
//                addLikeCount(detail);
            addTitleTabItem(detail);
        }
//        }


        List<PbBean.PbBaseComment> comments = detail.getCommentListList();
        if (comments != null && comments.size() > 0) {
            for (int i = 0; i < comments.size(); i++) {
                PbBean.PbBaseComment comment = comments.get(i);
//                addCommentItem(comment);
            }
        }

        List<PbBean.PbUserSimple> likes = detail.getLikeListList();
        if (likes != null && likes.size() > 0) {
            addThumbUserLikeItem(detail.getDetail().getId(), likes);
        }
//        if (likes != null && likes.size() > 0) {
//            for (int i = 0; i < likes.size(); i++) {
//                PbBean.PbUserSimple like = likes.get(i);
//                addLikeItem(like);
//            }
//        }

        notifyDataSetChanged();
    }

    public void refreshGuideCommentCount(int commentCount) {
        for (GuideDetailItem item : mItems) {
            if (item.itemType == GuideDetailItem.TYPE_LIKE_SECTION) {
                item.commentCount = commentCount;
                break;
            }
        }
    }

//    //刷新评论
//    public void refreshComments(List<PbBean.PbBaseComment> comments) {
//        mItemsTab0.clear();
//        if (comments != null && comments.size() > 0) {
//            for (int i = 0; i < comments.size(); i++) {
//                PbBean.PbBaseComment comment = comments.get(i);
////                addCommentItem(comment);
//            }
//        }
//        notifyDataSetChanged();
//    }

//    //加载更多评论
//    public void loadMoreComments(List<PbBean.PbBaseComment> comments) {
//        if (comments != null && comments.size() > 0) {
//            for (int i = 0; i < comments.size(); i++) {
//                PbBean.PbBaseComment comment = comments.get(i);
////                addCommentItem(comment);
//            }
//        }
//        notifyDataSetChanged();
//    }
//
    public void onEventMainThread(GuideDetailEvent event) {
        if (event != null && GuideDetailEvent.EVENT_DO_SORT == event.getEvent()) {
            for (GuideDetailItem item : mItems) {
                if (item.itemType == GuideDetailItem.TYPE_INTERACTIONT_TITLE) {
                    item.sortType = event.getSortType();
                    break;
                }
            }
        }
    }

    //刷新点赞
    public void refreshLikes(List<PbBean.PbUserSimple> likes) {
        mItemsTab1.clear();
        if (likes != null && likes.size() > 0) {
            for (int i = 0; i < likes.size(); i++) {
                PbBean.PbUserSimple like = likes.get(i);
                addLikeItem(like);
            }
        }
        notifyDataSetChanged();
    }

    private void addUserItem(PbBean.PbResGuideDetail detail) {
        GuideDetailItem item;
        List<PbBean.PbImage> imageList = detail.getDetail().getImageListList();
        if (imageList == null || imageList.size() <= 0) {
            item = new GuideDetailItem(GuideDetailItem.TYPE_USER_TXT);
        } else if (imageList.size() == 1) {
            item = new GuideDetailItem(GuideDetailItem.TYPE_USER_SINGLE_IMAGE);
        } else {
            item = new GuideDetailItem(GuideDetailItem.TYPE_USER_MULTI_IMAGE);
        }
        item.guideSimple = detail.getDetail();
        item.likeCount = detail.getDetail().getLikeCount();
        item.commentCount = detail.getDetail().getCommentCount();
        item.interactionCount = detail.getDetail().getInteractionCount();
        item.evaluation = detail.getDetail().getEvaluation();
        item.evaluationTime = detail.getDetail().getEvaluationTime();

        mItems.add(item);
    }

    private void addUserShowItem(PbBean.PbUserSimple userShow, PbBean.PbGuideSimple guideSimple) {
        if (userShow == null || guideSimple.getStatus() == PbBean.PbSignGuideStatus.GUIDE_STATUS_WAIT) {
            GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_USERSHOW);
            item.designerInfo = userShow;
            mItems.add(item);
        } else {
            return;
        }
    }

    private void addInteractionTitleItem(PbBean.PbResGuideDetail detail) {
        GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_INTERACTIONT_TITLE);
        item.interactionCount = detail.getDetail().getInteractionCount();
        item.evaluation = detail.getDetail().getEvaluation();
        item.evaluationTime = detail.getDetail().getEvaluationTime();
        item.sortType = "按时间降序";
        mItems.add(item);
    }

    public GuideDetailItem getInteractionTitleItem(PbBean.PbResGuideDetail detail) {
        GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_INTERACTIONT_TITLE);
        item.interactionCount = detail.getDetail().getInteractionCount();
        item.evaluation = detail.getDetail().getEvaluation();
        item.evaluationTime = detail.getDetail().getEvaluationTime();
        item.sortType = "按时间降序";
        return item;
    }

    private void addInteractionItem(PbBean.PbGuideInteraction interaction, boolean isStylist, PbBean.PbSignGuideStatus pbSignGuideStatus) {
        GuideDetailItem item;
        List<PbBean.PbImage> imageList = interaction.getImageListList();
        if (imageList == null || imageList.size() <= 0) {
            item = new GuideDetailItem(GuideDetailItem.TYPE_INTERACTION_TXT);
        } else if (imageList.size() == 1) {
            item = new GuideDetailItem(GuideDetailItem.TYPE_INTERACTION_SINGLE_IMAGE);
        } else {
            item = new GuideDetailItem(GuideDetailItem.TYPE_INTERACTION_MULTI_IMAGE);
        }
        item.pbSignGuideStatus = pbSignGuideStatus;
        item.interaction = interaction;
        item.isStylist = isStylist;
        mItems.add(item);
    }

    private void addTitleTabItem(PbBean.PbResGuideDetail detail) {
        GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_LIKE_SECTION);
        item.commentCount = detail.getCommentListCount();
        item.likeCount = detail.getLikeListCount();
        item.currentTab = mCurrentTab;

        mItems.add(item);
    }

//    public  void addLikeCount(int likeCount){
//        GuideDetailItem item=new GuideDetailItem(GuideDetailItem.TYPE_LIKE_SECTION);
//        item.likeCount = likeCount;
//        item.currentTab = GuideDetailItem.GuideTab.LIKE;
//        mItems.add(item);
//    }

//    private void addCommentItem(PbBean.PbBaseComment comment) {
//        GuideDetailItem item;
//        List<PbBean.PbImage> imageList = comment.getImageListList();
//        if (imageList == null || imageList.size() <= 0) {
//            item = new GuideDetailItem(GuideDetailItem.TYPE_COMMENT_TXT);
//        } else if (imageList.size() == 1) {
//            item = new GuideDetailItem(GuideDetailItem.TYPE_COMMENT_SINGLE_IMAGE);
//        } else {
//            item = new GuideDetailItem(GuideDetailItem.TYPE_COMMENT_MULTI_IMAGE);
//        }
//        item.comment = comment;
//
//        mItemsTab0.add(item);
//    }

    private void addLikeItem(PbBean.PbUserSimple like) {
        GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_LIKE);
        item.like = like;
        mItemsTab1.add(item);
    }

    private void addThumbUserLikeItem(int answerId, List<PbBean.PbUserSimple> likes) {
        GuideDetailItem item = new GuideDetailItem(GuideDetailItem.TYPE_GUIDE_LIKE);
        item.likeInfos = likes;
        item.answerId = answerId;
        mItems.add(item);
    }

    @Override
    public GuideDetailItem getItem(int position) {
        if (position < mItems.size()) {
            return mItems.get(position);
        } else if (getCurrentTab() == GuideDetailItem.GuideTab.COMMENT) {
            return mItemsTab0.get(position - mItems.size());
        } else {
            return mItemsTab1.get(position - mItems.size());
        }
    }

    @Override
    public int getCount() {
        int size = getCurrentTab() == GuideDetailItem.GuideTab.COMMENT ? mItemsTab0.size() : mItemsTab1.size();
        return mItems.size() + size;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).itemType;
    }

    @Override
    public int getViewTypeCount() {
        return 15;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case GuideDetailItem.TYPE_USER_TXT:
                return new GuideUserTxtViewHolder(mInflater.inflate(R.layout.item_guide_user_txt, parent, false));
            case GuideDetailItem.TYPE_USER_SINGLE_IMAGE:
                return new GuideUserSingleImageViewHolder(mInflater.inflate(R.layout.item_guide_user_multi_image, parent, false));
            case GuideDetailItem.TYPE_USER_MULTI_IMAGE:
                return new GuideUserMultiImageViewHolder(mInflater.inflate(R.layout.item_guide_user_multi_image, parent, false));
            case GuideDetailItem.TYPE_USERSHOW:
                return new GuideDesignerShowViewHolder(mInflater.inflate(R.layout.item_guide_designer_show, parent, false));
            case GuideDetailItem.TYPE_INTERACTIONT_TITLE:
                return new GuideInteractionTitleViewHolder(mInflater.inflate(R.layout.item_guide_interaction_title, parent, false));
            case GuideDetailItem.TYPE_INTERACTION_TXT:
                return new GuideInteractionTxtViewHolder(mInflater.inflate(R.layout.item_comment_txt, parent, false));
            case GuideDetailItem.TYPE_INTERACTION_SINGLE_IMAGE:
                return new GuideInteractionSingleImageViewHolder(mInflater.inflate(R.layout.item_comment_multi_image, parent, false));
            case GuideDetailItem.TYPE_INTERACTION_MULTI_IMAGE:
                return new GuideInteractionMultiImageViewHolder(mInflater.inflate(R.layout.item_comment_multi_image, parent, false));
            case GuideDetailItem.TYPE_TITLE_TAB:
                return new GuideTitleTabViewHolder(mInflater.inflate(R.layout.item_guide_text_title_tab, parent, false));
            case GuideDetailItem.TYPE_LIKE_SECTION:
                return new GuideSectionLikeViewHolder(mInflater.inflate(R.layout.view_like_section, parent, false));
            case GuideDetailItem.TYPE_COMMENT_TXT:
                return new GuideCommentTxtViewHolder(mInflater.inflate(R.layout.item_comment_txt, parent, false));
            case GuideDetailItem.TYPE_COMMENT_SINGLE_IMAGE:
                return new GuideCommentSingleImageViewHolder(mInflater.inflate(R.layout.item_comment_single_image, parent, false));
            case GuideDetailItem.TYPE_COMMENT_MULTI_IMAGE:
                return new GuideCommentMultiImageViewHolder(mInflater.inflate(R.layout.item_comment_multi_image, parent, false));
            case GuideDetailItem.TYPE_LIKE:
                return new GuideLikeViewHolder(mInflater.inflate(R.layout.item_guide_like, parent, false));
            case GuideDetailItem.TYPE_GUIDE_LIKE:
                return new GuideThumbLikeViewHolder(mInflater.inflate(R.layout.item_reply_thumb_like, parent, false));

        }
        return null;
    }
}
